package com.example.s3demo.utils

import java.math.BigDecimal

object ConvertOp {
    fun isNull(obj: Any?) = obj == null

    fun convert2String(obj: Any?): String {
        var result: String
        if (!isNull(obj)) {
            try {
                result = obj.toString()
            } catch (e: Exception) {
                e.printStackTrace()

                result = ""
            }
        } else {
            result = ""
        }
        return result
    }

    fun convert2Int(obj: Any?): Int {
        var result: Int
        if (!isNull(obj)) {
            if (obj!!.javaClass == BigDecimal::class.java) {
                val bigDecimal = obj as BigDecimal
                return bigDecimal.toInt()
            } else {
                try {
                    result = obj.toString().toInt()
                } catch (e: Exception) {
                    e.printStackTrace()

                    result = 0
                }
            }
        } else {
            result = 0
        }
        return result
    }
}
